/*
 * Sales Data Controller Project.
 * Copyright (C) 2011 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.sdc.vsc.tlv;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TLVContext<T> {

	private static final Log LOG = LogFactory.getLog(TLVContext.class);

	private final TLVMarshaller<T> tlvMarshaller;

	private static final Map<Class<?>, TLVCodec<?>> tlvCodecs;

	static {
		tlvCodecs = new HashMap<Class<?>, TLVCodec<?>>();
		/*
		 * Tried to use scannotation here. But stumbled upon recursive loading
		 * issue.
		 */
		tlvCodecs.put(Byte.TYPE, new ByteTLVCodec());
		tlvCodecs.put(Long.TYPE, new LongTLVCodec());
		tlvCodecs.put(byte[].class, new ByteArrayTLVCodec());
	}

	private TLVContext(Class<T> tlvClass) {
		TLVMetadata tlvMetadata = new TLVMetadata(tlvClass);
		this.tlvMarshaller = new TLVMarshaller<T>(tlvMetadata, tlvCodecs);
	}

	public static <T> TLVContext<T> newInstance(Class<T> tlvClass) {
		TLVContext<T> tlvContext = new TLVContext<T>(tlvClass);
		return tlvContext;
	}

	public TLVMarshaller<T> getMarshaller() {
		return this.tlvMarshaller;
	}
}
